/*
 *
 * @ProjectName:    my_umi_app
 * @Package:
 * @ClassName:      list
 * @Description:    留言列表
 * @Author:         SuPeiSen
 * @CreateDate:     2021/3/2 2:59 下午
 * @UpdateDate:     2021/3/2 2:59 下午
 * @UpdateRemark:   更新说明：
 * @Version:        1.0
 */

import ProTable, { ProColumns } from '@ant-design/pro-table';
import { message, Switch, Tag } from 'antd';
import { history } from '@@/core/history';
import { boolToNumOneOrZero, numberToBool } from '@/utils/app_fun';
import { SortOrder } from 'antd/lib/table/interface';
import React from 'react';
import { updateCategoryApi } from '@/service/category_api';
import {
  DeleteMessgaeApi,
  MessageListApi,
  UpdateMessageStatusApi,
} from '@/service/message_api';
import { MessageItemType } from '@/type/message_type';

const columns: ProColumns<MessageItemType>[] = [
  {
    title: 'id',
    dataIndex: 'id',
    key: 'id',
    search: false,
    editable: false,
  },
  {
    title: '父级id',
    dataIndex: 'parent_id',
    render: (dom, item) => {
      return item.parent_id === -1 ? (
        <Tag color={'blue'}>一级</Tag>
      ) : (
        <div>{item.parent_id}</div>
      );
    },
  },
  {
    title: '文章',
    render: (dom, item) => {
      return (
        <div style={{ fontWeight: 'bold', color: '#a45d9c' }}>
          {item.article.article_title}
        </div>
      );
    },
  },
  {
    title: '昵称',
    dataIndex: 'user_name',
    render: (dom, item) => {
      return <div style={{ color: '#3397ec' }}>{item.user_name}</div>;
    },
  },
  {
    title: '邮箱',
    dataIndex: 'user_email',
  },
  {
    title: '添加时间',
    dataIndex: 'add_time',
  },
  {
    title: '性别',
    dataIndex: 'user_sex',
    render: (dom, item) => {
      if (numberToBool(item.user_sex)) {
        // girl
        return <Tag color={'orange'}>girl</Tag>;
      }
      return <Tag color={'blue'}>boy</Tag>;
    },
  },
  {
    title: '身份',
    dataIndex: 'is_author',
    render: (dom, item) => {
      if (numberToBool(item.is_author)) {
        return <Tag color={'#000'}>作者</Tag>;
      }
      return <Tag>游客</Tag>;
    },
  },
  {
    title: '状态',
    dataIndex: 'read_status',
    render: (dom, item) => {
      if (item.read_status === 0) {
        return <Tag color={'red'}>未读</Tag>;
      }
      if (item.read_status === 1) {
        return <Tag color={'lime'}>已回复</Tag>;
      }
      return <Tag color={'geekblue'}>回复</Tag>;
    },
  },
  {
    title: '操作',
    valueType: 'option',
    render: (text, record, _, action) => [
      <a
        target="_blank"
        key={'delete_' + `${record.id}`}
        onClick={() => {
          deleteShareUrl(record.id, action);
        }}
      >
        删除
      </a>,

      <a
        target="_blank"
        key={'delete_' + `${record.id}`}
        onClick={() => {
          history.push({
            pathname: '/admin/message/answer_message',
            query: { id: record.id, article_id: record.article.id },
          });
        }}
      >
        回复
      </a>,

      <Switch
        key={record.id}
        checkedChildren="通过"
        unCheckedChildren="拒绝"
        defaultChecked={numberToBool(record.status)}
        onChange={async (checked, event) => {
          const success = await UpdateMessageStatusApi({
            id: record.id,
            status: boolToNumOneOrZero(checked),
          });
          if (success) {
            message.success('更新成功');
          } else {
            message.error('更新失败');
          }
        }}
      />,
    ],
  },
];

const deleteShareUrl = async (id: number, action: any) => {
  const success = await DeleteMessgaeApi(id);
  if (success) {
    action.reload();
    message.success('删除成功');
  } else {
    message.error('删除失败');
  }
};

export default () => {
  const getMessageList = async (
    params: {
      pageSize?: number;
      current?: number;
      keyword?: string;
    },
    sort: {
      [key: string]: SortOrder;
    },
    filter: {
      [key: string]: React.ReactText[];
    },
  ) => {
    const model = await MessageListApi({
      page: params.current ?? 1,
      limit: params.pageSize ?? 10,
    });
    if (model.success) {
      return {
        success: true,
        data: model.data!.data.lists,
        total: model.data!.data.total,
      };
    }
    return {
      data: [],
      success: false,
    };
  };

  return (
    <ProTable<MessageItemType>
      columns={columns}
      rowKey="id"
      request={getMessageList}
      editable={{
        onSave: async (key, row) => {
          console.log(row);
          const success = await updateCategoryApi(row);
          if (success) {
            message.success('成功');
          } else {
            message.error('失败');
          }
        },
      }}
      pagination={{ pageSize: 20 }}
    />
  );
};
